package com.site.dao;

import com.site.common.DbHelper;
import com.site.entity.NewsEntity;

import javax.swing.text.html.parser.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDao extends DbHelper{
    public NewsDao() throws ClassNotFoundException, SQLException {
    }

    //得到所有的新闻信息
    public List<NewsEntity> getAll() throws SQLException {
       ResultSet rs= query("select * from tbNews");
       List<NewsEntity> list=new ArrayList<NewsEntity>();
       while (rs.next()){
           list.add(getEntity(rs));
       }

       rs.close();
       close();
       return list;
    }


    public List<NewsEntity> getlist(String where) throws SQLException {
        String sql="select * from tbNews";
        if(where.length()>0){
            sql+=" where "+where;
        }

        ResultSet rs= query(sql);
        List<NewsEntity> list=new ArrayList<NewsEntity>();
        while (rs.next()){
            list.add(getEntity(rs));
        }

        rs.close();
        close();
        return list;
    }

    private NewsEntity getEntity(ResultSet rs) throws SQLException {
        NewsEntity entity=new NewsEntity();
        entity.setContent(rs.getString("content"));
        entity.setCounter(rs.getInt("counter"));
        entity.setDescription(rs.getString("description"));
        entity.setId(rs.getInt("id"));
        entity.setImg(rs.getString("img"));
        entity.setIsTop(rs.getInt("isTop"));
        entity.setPushTime(rs.getDate("pushTime"));
        entity.setTid(rs.getInt("tId"));
        entity.setTitle(rs.getString("title"));
        return entity;
    }
}
